package xcmg.device.web.order;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.order.SubcontractingOutService;
import xcmg.device.service.vo.order.SubcontractingOutVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


@RestController
@RequestMapping("/subcontractingOut")
@Api(tags = "转包出库单")
@Slf4j
public class SubcontractingOutController extends BasicController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SubcontractingOutService subcontractingOutService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult<PageInfo<SubcontractingOutVO>> list(@RequestBody SearchCommonVO<SubcontractingOutVO> condition) throws Exception {
        SubcontractingOutVO subcontractingOutVO = condition.getFilters();
        if (subcontractingOutVO == null) {
            subcontractingOutVO = new SubcontractingOutVO();
        }
        if (subcontractingOutVO.getOrgId() == null) {
            subcontractingOutVO.setOrgId(getOrgId());
        }
        condition.setFilters(subcontractingOutVO);
        PageCommonVO<SubcontractingOutVO> pageCommonVO = subcontractingOutService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/item", method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult<SubcontractingOutVO> item(String id) {
        return actionValueResult(subcontractingOutService.getVoById(id));
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult<String> add(@RequestBody SubcontractingOutVO v) throws Exception {
        v.setUpdateId(getUserId(request));
        v.setCreateId(getUserId(request));
        v.setUpdateName(getUserName());
        v.setOrgId(getOrgId());
        boolean f = subcontractingOutService.saveSubcontractingOut(v);
        ErrorCode code = f ? ErrorCode.Success : ErrorCode.Failure;
        return actionResult(code);
    }

    @RequestMapping(value = "/updateArea", method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult<String> updateArea(@RequestBody SubcontractingOutVO v) throws Exception {
        if (StringUtils.isEmpty(v.getId()) || StringUtils.isEmpty(v.getOutArea()) || StringUtils.isEmpty(v.getOutWarehouse())) {
            return actionResult(ErrorCode.IllegalArument);
        }
        v.setUpdateId(getUserId(request));
        v.setUpdateName(getUserName());
        v.setUpdatedTime(new Date());
        v.setOrgId(getOrgId());
        boolean code = subcontractingOutService.updateArea(v);
        return code ? actionResult(ErrorCode.Success) : actionResult(ErrorCode.Failure);
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult<String> delete(@RequestBody SubcontractingOutVO subcontractingOutVO) {
        if (subcontractingOutVO.getId() == null) {
            return actionResult(ErrorCode.IllegalArument);
        }
        subcontractingOutVO.setUpdateId(getUserId(request));
        boolean code = subcontractingOutService.delByIds(subcontractingOutVO.getId(), getUserId());
        return code ? actionResult(ErrorCode.Success) : actionResult(ErrorCode.Failure);
    }
}
